{% extends 'layout/manage.html' %}
{% load static %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.min.css' %}">
    <style>

        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .panel-body {
            padding: 0;
        }

        .title-list{
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .title-list ul{
            padding-left: 15px;
        }
        .title-list ul a{
            display: block;
            padding: 5px 0;
        }
        .content{
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
        /*全屏显示*/
        .editormd-fullscreen{
            z-index: 1001;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid">
            <div class="panel panel-default">
              <div class="panel-heading">
                  <div>
                    <i class="fa fa-book" aria-hidden="true"></i>&nbsp;&nbsp;wiki文档
                  </div>
                  <div class="function">
                      <a href="{% url 'wiki_add' project_id=request.tracer.project.id %}" type="button" class="btn btn-success btn-xs">
                          <i class="fa fa-plus-circle"></i> 新建
                      </a>
                  </div>
              </div>
              <div class="panel-body">
                  <div class="col-sm-3 title-list">
                      <ul id="catalog">
                      </ul>
                  </div>
                  <div class="col-sm-9 content">
                      <div >
                          <form method="post">
                              {% csrf_token %}
                              {% for feild in form %}
                                  {% if feild.name == "content" %}
{#                                      找到textarea#}
                                  <div class="form-group">
                                      <label for="{{ feild.id_for_label }}">{{ feild.label }}</label>
                                      <div id="editor">
                                          {{ feild }}
                                      </div>
                                      <span class="error-msg">{{ feild.errors.0 }}</span>
                                  </div>

                                  {% else %}
                                  <div class="form-group">
                                      <label for="{{ feild.id_for_label }}">{{ feild.label }}</label>
                                      {{ feild }}
                                      <span class="error-msg">{{ feild.errors.0 }}</span>
                                  </div>
                                  {% endif %}
                              {% endfor %}
                              <button type="submit" class="btn btn-primary">提 交</button>
                            </form>
                      </div>
                  </div>
              </div>
        </div>
    </div>

{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script>
    var WIKI_DETAIL_URL = "{% url 'wiki' project_id=request.tracer.project.id %}";
    var WIKI_UPLOAD_URL = "{% url 'wiki_upload' project_id=request.tracer.project.id %}";
        $(function () {
            initCatalog();
            initEditMd();
        });

        function initCatalog() {
            $.ajax({
                url: '{% url "catalog" project_id=request.tracer.project.id %}',
                type: 'GET',
                dataType: 'JSON',
                success: function (res) {
                    if(res.status){
                        $.each(res.data, function (index, item) {
                            let href = WIKI_DETAIL_URL + "?wiki_id=" + item.id;
                            let li = $("<li>").attr('id', 'id_' + item.id).append($('<a>').text(item.title).attr('href', href)).append($('<ul>'));
                            if(!item.parent){
                                // 没有父id
                                // 生成li标签，添加到catalog
                                $('#catalog').append(li)
                            }
                            else {
                                $('#id_' + item.parent).children('ul').append(li)
                            }
                        })
                    }
                    else{
                        alert("初始化目录失败！")
                    }

                }
            })
        }

        // 初始化md编辑器，将textarea转换为md
        function initEditMd() {
            editormd('editor', {
                placeholder: "请输入内容",
                height:500,
                path: "{% static 'plugin/editor-md/lib/' %}", //组件路径
                imageUpload: true, // 本地上传图片
                imageFormats: ["jpg", "jpeg", "png", "gif", "bmp"],
                imageUploadURL: WIKI_UPLOAD_URL,
                emoji: true,
                taskList: true,
                tocm: true,
                tex: true, // 默认不解析
                flowChart: true, // 默认不解析
                sequenceDiagram: true, // 默认不解析
                codeFold: true,
            })
        }
    </script>
{% endblock %}